package gvjclient.ui;
/*
Copyright 2010 Arpit Sud
This file is part of gv-jclient. (http://code.google.com/p/gv-jclient/)
gv-jclient is an Unofficial Java client for Google Voice.

gv-jclient is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.

gv-jclient is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with gv-jclient.  If not, see <http://www.gnu.org/licenses/>.
*/

import gvjclient.GVJclient;

import java.awt.Component;
import java.io.File;

import javax.swing.ImageIcon;
import javax.swing.JTree;
import javax.swing.tree.DefaultMutableTreeNode;
import javax.swing.tree.DefaultTreeCellRenderer;

public class AccountsTreeRenderer extends DefaultTreeCellRenderer {

	ImageIcon loggedInIcon = null;
	ImageIcon loggedOutIcon = null;
	
	public AccountsTreeRenderer()
	{
        loggedInIcon = new ImageIcon(this.getClass().getResource(File.separator + "images" + File.separator + "loggedIn.png"));
        loggedOutIcon = new ImageIcon(this.getClass().getResource(File.separator + "images" + File.separator + "loggedOut.png"));
	}
	
 /*   private ImageIcon createImageIcon(String path) {
        File imageFile = new File(path);
        if (imgURL != null) {
            return new ImageIcon(imgURL);
        } else {
            System.err.println("Couldn't find file: " + path);
            return null;
        }
    }*/

	
    public Component getTreeCellRendererComponent(JTree tree, Object value, boolean sel,
            									  boolean expanded, boolean leaf, int row,
            									  boolean hasFocus)
    {

    	super.getTreeCellRendererComponent( tree, value, sel, expanded, leaf, row, hasFocus);
    	
    	try
    	{
       		DefaultMutableTreeNode node = (DefaultMutableTreeNode)value;
       		GVJclient client = (GVJclient)(node.getUserObject());
       		if(client.isLoggedIn())
       		{
       			this.setIcon(loggedInIcon);
       		}
       		else
       		{
       			this.setIcon(loggedOutIcon);
       		}
    	} catch(ClassCastException ex)
    	{
    	
    	} finally
    	{
    		this.setToolTipText(this.getAccountUsername(value));
    	}
    	return this;
    }

    private String getAccountUsername(Object value)
    {
       	try
    	{
    		DefaultMutableTreeNode node = (DefaultMutableTreeNode)value;
    		GVJclient client = (GVJclient)(node.getUserObject());
    		return client.getAccountUsername();
    	} catch(ClassCastException ex)
    	{
    		return null;
    	}
    }
}

